package com.kgc.fk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.OtherPay;
import com.kgc.collection.pojo.OtherPayForInfo;
import com.kgc.collection.pojo.OtherPayWay;
import com.kgc.fk.dao.OtherPayDao;
import com.kgc.fk.service.OtherPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OtherPayServiceImpl implements OtherPayService {

    private Logger logger = LoggerFactory.getLogger(OtherPayServiceImpl.class);

    @Resource
    private OtherPayDao otherPayDao;



    @Override
    public ResponseMsg getOtherPayPage(Map<String, Object> map) {
        logger.info("getOtherPayPage servImpl map{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("201");
        int pageNum = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNum =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNum = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<OtherPay> otherPayList = otherPayDao.getOtherPayList(map);
        PageInfo<OtherPay> pageInfo = new PageInfo<>(otherPayList);
        responseMsg.setData(pageInfo);
        responseMsg.setCode("200");
        logger.info("getOtherPayPage servImpl map{} , response{}" ,map,responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg addOtherPay(Map<String, Object> map) {
        logger.info("addOtherPay servImpl map{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("500");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("payInfo");
        ArrayList<Map<String, Object>> accountList =(ArrayList<Map<String, Object>>) map.get("payWay");

        Date date = new Date();
        String code = map.get("code").toString();
        map.put("createDate", date);
        Object createId = map.get("createId");

        Double debtAmount = Double.valueOf( map.get("totalAmount").toString()) -  Double.valueOf( map.get("payAmount").toString()) ;
        map.put("debtAmount", debtAmount);
        int result = otherPayDao.addOtherPay(map);

        logger.info("addOtherPay result:{}",result);
        if (result <= 0) {
            return responseMsg;
        }
        for (Map<String, Object> info : infoList) {
            info.put("createDate", date);
            info.put("majorCode", code);
            info.put("createId", createId);
            result = otherPayDao.addPayInfo(info);
            if (result <= 0) {
                return responseMsg;
            }
        }

        for (Map<String,Object> account : accountList) {
            account.put("createDate", date);
            account.put("majorCode", code);
            account.put("createId", createId);
            result = otherPayDao.addPayAccount(account);
            if (result <= 0) {
                return responseMsg;
            }
        }
        responseMsg.setCode("200");
        logger.info("addOtherPay end response:{} , map{}",responseMsg,map);
        return responseMsg;
    }

    @Override
    public ResponseMsg getQTFK(Map<String, Object> params) {
        logger.info("getQTFK servImpl map{}" ,params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("500");
        String code = (String) params.get("code");
        List<OtherPay> otherPayList = otherPayDao.getOtherPayList(params);
        for (OtherPay otherPay : otherPayList) {
            List<OtherPayWay> list = otherPayDao.getOtherPayWay(code);
            List<OtherPayForInfo> infoList = otherPayDao.getOtherPayForInfo(code);
            otherPay.setPayForList(infoList);
            otherPay.setPayWayList(list);
        }
        responseMsg.setData(otherPayList.get(0));
        responseMsg.setCode("200");
        logger.info("getQTFK servImpl map{} response{}",params,responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateOtherPay(Map<String, Object> map) {
        logger.info("updateOtherPay servImpl map{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("500");
        String code = (String) map.get("code");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("payInfo");
        ArrayList<Map<String, Object>> accountList =(ArrayList<Map<String, Object>>) map.get("payWay");
        Date date = new Date();
        Object modiId = map.get("modiId");
        Double debtAmount = Double.valueOf(map.get("totalAmount").toString() )-  Double.valueOf(map.get("payAmount").toString()) ;
        map.put("debtAmount", debtAmount);
        map.put("modiId", modiId);
        map.put("modiDate", date);
        int result = otherPayDao.modiOtherPay(map);
        logger.info("updateOtherPay result:{}",result);
        if (result <= 0) {
            responseMsg.setMsg("修改主订单失败");
            return responseMsg;
        }
        result = otherPayDao.delOtherPayWay(code);
        logger.info("delOtherPayWay result:{}",result);
        result = otherPayDao.delOtherPayForInfo(code);
        logger.info("delOtherPayForInfo result:{}",result);

        for (Map<String, Object> info : infoList) {
            info.put("createDate", date);
            info.put("majorCode", code);
            info.put("createId", modiId);
            result = otherPayDao.addPayInfo(info);
            logger.info("addOtherPayInfo result:{}",result);
            if (result <= 0) {
                responseMsg.setMsg("修改子订单 addPayInfo 失败"  );
                return responseMsg;
            }
        }

        for (Map<String,Object> account : accountList) {
            account.put("createDate", date);
            account.put("majorCode", code);
            account.put("createId", modiId);
            result = otherPayDao.addPayAccount(account);
            logger.info("addOtherPayAccount result:{}",result);
            if (result <= 0) {
                responseMsg.setMsg("修改付款账户失败");
                return responseMsg;
            }
        }
        responseMsg.setCode("200");
        logger.info("updateOtherPay end response:{} , map{}",responseMsg,map);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateQTFK(Map<String, Object> params) {
        logger.info("updateQTFK start params:{}",params);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("500");
        int result = otherPayDao.updateOtherPayStatus(params)  ;
        logger.info("updateOtherPay params{} result:{}",params,result);
        if (result > 0 ){
            responseMsg.setCode("200");
        }
        logger.info("updateOtherPay end response:{} , map{}",responseMsg,params);
        return responseMsg;
    }

    @Override
    public ResponseMsg getQTFKList(Map<String, Object> params) {
        logger.info("getQTFKList start params:{}",params);
        ResponseMsg responseMsg = new ResponseMsg();

        List<OtherPay> otherPayList = otherPayDao.getOtherPayList(params);

//        for (OtherPay otherPay : otherPayList) {
//            List<OtherPayWay> list = otherPayDao.getOtherPayWay(otherPay.getCode());
//            List<OtherPayForInfo> infoList = otherPayDao.getOtherPayForInfo(otherPay.getCode());
//            otherPay.setPayForList(infoList);
//            otherPay.setPayWayList(list);
//        }
        responseMsg.setData(otherPayList);
        responseMsg.setCode("200");
        logger.info("getQTFKList end response:{}",responseMsg);
        return responseMsg;
    }


}
